package com.lvhr.rpc.filter;


import com.lvhr.rpc.model.RpcRequest;
import lombok.extern.slf4j.Slf4j;

/**
 * 时间戳校验器
 */

@Slf4j
public class TimestampValidator implements SecurityValidator {
    private final long maxTimeDiff;
    public TimestampValidator(long maxTimeDiff) {
        this.maxTimeDiff = maxTimeDiff;
    }

    public boolean validate(RpcRequest request) throws SecurityException {
        long currentTime = System.currentTimeMillis();
        long requestTime = request.getAttachment("timestamp");
        if (Math.abs(currentTime - requestTime) > 60000) {
            throw new SecurityException(400,"请求已过期");
        }
        return true;
    }

}
